﻿using FriendStar.Application.ProductionPlan;
using FriendStar.Code;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Mvc;

namespace FriendStar.Web.Areas.ProductionPlan.Controllers
{
    public class PreparationMaterialsController : Controller
    {
        private string useUnit = Configs.GetValue("UseUnit");

        private string factoryFlag = Configs.GetValue("FactoryFlag");

        private string curUser = OperatorProvider.Provider.GetCurrent().UserName;

        private string UseUnit = Configs.GetValue("UseUnit");

        PreparationMaterialsApp app = new PreparationMaterialsApp();
        //
        // GET: /ProductionPlan/PreparationMaterials/PreparationMaterialsPlan
        public ActionResult Index()
        {
            return View();
        }
        // GET: /ProductionPlan/PreparationMaterials/PreparationMaterialsPlan
        public ActionResult PreparationMaterialsPlan()
        {
            return View();
        }
        // GET: /ProductionPlan/PreparationMaterials/MaterialOperation
        public ActionResult MaterialOperation()
        {
            return View();
        }
        // GET: /ProductionPlan/PreparationMaterials/MaterialOperationWaste
        public ActionResult MaterialOperationWaste()
        {
            return View();
        }
        // GET: /ProductionPlan/PreparationMaterials/MaterialWaste
        public ActionResult MaterialWaste()
        {
            return View();
        }
        // GET: /ProductionPlan/PreparationMaterials/PreparationMaterialsStatistics
        public ActionResult PreparationMaterialsStatistics()
        {
            return View();
        }

        public ActionResult MaterialOperationDetails(string MatNO,string archive)
        {
            bool isArchive = "1" == archive;
            var data = app.GetMaterialOperationDetails(MatNO, UseUnit,isArchive);

            ViewData["jsonData"] = data.ToJson();

            ViewData["MatNo"] = (data as Dictionary<string, object>)["MatNo"].ToString();

            return View();
        }

        public ActionResult GetRequireCompanies()
        {
            var data = app.GetRequireCompanies(useUnit);

            return Content(data.ToJson());
        }


        public ActionResult GetMaterialOperationsEdit(Pagination pagination, string ProductMonth, string MaintainStatus, string RequireCompanyNO, string MatNO, string ProductionNO, string FProductionName, string MaterialCode_SJJL, string MaterialSizes_SJJL)
        {
            var data = new
            {
                rows = app.GetMaterialOperationsEdit(pagination, ProductMonth, MaintainStatus, RequireCompanyNO, MatNO, ProductionNO, FProductionName, MaterialCode_SJJL, MaterialSizes_SJJL, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult GetMaterialOperationsArchive(Pagination pagination, string ProductMonth, string MaintainStatus, string RequireCompanyNO, string MatNO, string ProductionNO, string FProductionName, string MaterialCode_SJJL, string MaterialSizes_SJJL)
        {
            var data = new
            {
                rows = app.GetMaterialOperationsArchive(pagination, ProductMonth, MaintainStatus, RequireCompanyNO, MatNO, ProductionNO, FProductionName, MaterialCode_SJJL, MaterialSizes_SJJL, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        //DeleteMaterialOperations
        public ActionResult DeleteMaterialOperation(string id)
        {
            var data = app.DeleteMaterialOperation(id, useUnit);

            return Content(data.ToString());
        }
        //InvalidMaterialOperations
        public ActionResult InvalidMaterialOperation(string id)
        {
            var data = app.InvalidMaterialOperation(id, useUnit);

            return Content(data.ToString());
        }
        public ActionResult GetMaterialOperationPrintData(string MatNO, bool isArchive)
        {
            var data = app.GetMaterialOperationPrintData(MatNO, isArchive, useUnit);

            return Content(data.ToJson());
        }
        public ActionResult PublishMaterialOperations(string ids)
        {
            DateTime now = DateTime.Now;

            string curDate = now.ToString("yyyy-MM-dd");

            string curTime = now.ToString("HH:mm:ss");

            var data = app.PublishMaterialOperations(ids, useUnit, curUser, curDate, curTime);

            return Content(data.ToString());
        }

        public ActionResult GetMaterialOperationImage(string Pic_ID, string IsImgStr)
        {
            MemoryStream ms = new MemoryStream();

            Bitmap bmp = null;

            bool IsImg = IsImgStr.Equals("1");

            if (IsImg)
            {//如果为1则为工艺图从数据表里取出
                bmp = app.GetMaterialOperationImage(Pic_ID);
            }
            else
            {

            }


            bmp.Save(ms, ImageFormat.Jpeg);

            return File(ms.ToArray(), "image/jpeg");
        }
    }
}
